package com.service.gmall.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.gmall.common.constant.SysRedisConst;
import com.service.gmall.feign.search.SearchFeignClient;
import com.service.gmall.model.list.Goods;
import com.service.gmall.model.list.SearchAttr;
import com.service.gmall.model.product.*;
import com.service.gmall.model.to.CategoryViewTo;
import com.service.gmall.model.to.SkuDetailsTo;
import com.service.gmall.product.mapper.BaseCategory3Mapper;
import com.service.gmall.product.mapper.BaseTrademarkMapper;
import com.service.gmall.product.service.*;
import com.service.gmall.product.mapper.SkuInfoMapper;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author alpha
 * @description 针对表【sku_info(库存单元表)】的数据库操作Service实现
 * @createDate 2022-08-23 10:15:19
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
        implements SkuInfoService {
    @Autowired
    SkuInfoMapper skuInfoMapper;
    @Autowired
    BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    SkuAttrValueService skuAttrValueService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    RedissonClient redissonClient;//注入分布式锁

    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    SearchFeignClient searchFeignClient;

    @Deprecated
    @Override
    public SkuDetailsTo getSkuDetails(Long skuId) {
        SkuDetailsTo skuDetailsTo = new SkuDetailsTo();
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);

        //查询商品基本信息
        skuDetailsTo.setSkuInfo(skuInfo);
        //查询出分级ID
        CategoryViewTo categoryViewTo = baseCategory3Mapper.getCategoryView(skuInfo.getCategory3Id());
        skuDetailsTo.setCategoryView(categoryViewTo);

        //查询价格
        BigDecimal realTimePrice = this.getRealTimePrice(skuId);
        skuDetailsTo.setPrice(realTimePrice);
        //查询sku图片信息
        List<SkuImage> skuImages = skuImageService.getSkImage(skuId);
        skuInfo.setSkuImageList(skuImages);
        //查询sku对应的属性值和名，并进行标记
        List<SpuSaleAttr> saleAttrList = spuSaleAttrService.getSaleAttrAndValueMarkSku(skuInfo.getSpuId(), skuId);
        skuDetailsTo.setSpuSaleAttrList(saleAttrList);

        //查询valuesSkuJson，商品（sku）的所有兄弟产品的销售属性名和值组合关系全部查出来，并封装成
        // {"118|120": "50","119|121": 50} 这样的json字符串
        Long spuId = skuInfo.getSpuId();
        String valueJson = spuSaleAttrService.getAllSkuSaleAttrValueJson(spuId);
        skuDetailsTo.setValuesSkuJson(valueJson);
        return skuDetailsTo;
    }

    @Override
    public BigDecimal getRealTimePrice(Long skuId) {
        BigDecimal price = skuInfoMapper.getRealTimePrice(skuId);
        return price;
    }

    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        //1.把 spu基本信息保存到 sku_info表中
        skuInfoMapper.insert(skuInfo);
        Long skuId = skuInfo.getId();
        //2.sku的图片信息保存到 sku_image
        List<SkuImage> skuImages = skuInfo.getSkuImageList();
        for (SkuImage skuImage : skuImages) {
            skuImage.setSkuId(skuId);
        }
        skuImageService.saveBatch(skuImages);
        //3.sku的平台属性名和值的关系保存到 sku_attr_value
        List<SkuAttrValue> attrValues = skuInfo.getSkuAttrValueList();
        for (SkuAttrValue attrValue : attrValues) {
            attrValue.setSkuId(skuId);
        }
        skuAttrValueService.saveBatch(attrValues);
        //4.sku的销售属性名和值的关系保存到 sku_sale_attr_value
        List<SkuSaleAttrValue> saleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        for (SkuSaleAttrValue saleAttrValue : saleAttrValueList) {
            saleAttrValue.setSkuId(skuId);
            saleAttrValue.setSpuId(skuInfo.getSpuId());
        }
        skuSaleAttrValueService.saveBatch(saleAttrValueList);

        //把skuId放到布隆过滤器中
        RBloomFilter<Object> filter = redissonClient.getBloomFilter(SysRedisConst.BLOOM_SKUID);
        filter.add(skuId);
    }

    @Override
    public void onsale(Long skuId) {
        //1、改数据库 sku_info 这个skuId的is_sale； 1上架  0下架
        skuInfoMapper.updateIsSale(skuId, 1);
        //todo 给es保存
        Goods goods = this.getGoodsBySkuId(skuId);
        searchFeignClient.saveGoods(goods);
    }

    @Override
    public void cancelSale(Long skuId) {
        skuInfoMapper.updateIsSale(skuId, 0);
        //todo 给es删除
        searchFeignClient.deleteGoods(skuId);
    }

    @Override
    public SkuInfo getSkuInfo(Long skuId) {
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
        return skuInfo;
    }

    @Override
    public List<SkuImage> getDetailImages(Long skuId) {
        List<SkuImage> skuImages = skuImageService.getSkImage(skuId);
        return skuImages;
    }

    @Override
    public List<Long> getAllSkuId() {
        return skuInfoMapper.getAllSkuId();
    }

    @Override
    public Goods getGoodsBySkuId(Long skuId) {
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);

        Goods goods = new Goods();

        goods.setId(skuId);
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());

        goods.setCreateTime(new Date());

        BaseTrademark trademark = baseTrademarkMapper.selectById(skuInfo.getTmId());
        goods.setTmId(skuInfo.getTmId());
        goods.setTmName(trademark.getTmName());
        goods.setTmLogoUrl(trademark.getLogoUrl());

        CategoryViewTo view = baseCategory3Mapper.getCategoryView(skuInfo.getCategory3Id());
        goods.setCategory1Id(view.getCategory1Id());
        goods.setCategory1Name(view.getCategory1Name());
        goods.setCategory2Id(view.getCategory2Id());
        goods.setCategory2Name(view.getCategory2Name());
        goods.setCategory3Id(view.getCategory3Id());
        goods.setCategory3Name(view.getCategory3Name());

        goods.setHotScore(0L);//todo 热度分更新

        //设置属性名和值
        List<SearchAttr> attrs = skuAttrValueService.getSkuAttrNameAndValue(skuId);
        goods.setAttrs(attrs);
        return goods;
    }
}





